package com.itany.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/11/14 11:18
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class Test11 {

    public static void main(String[] args) {
        selectAll();

        User user = new User("李白","123456","13112345678","王者荣耀");
        Integer id = insertReturnPrimaryKey(user);
        System.out.println("保存后返回的主键为:"+id);
    }

    public static List<User> selectAll(){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();
        try {
            conn = JDBCUtil.getConnection();

            // 获取数据库元数据
            // 通过连接可以获取到数据库的元数据
            DatabaseMetaData dbmd = conn.getMetaData();
            System.out.println("数据库产品名:"+dbmd.getDatabaseProductName());
            System.out.println("数据库版本:"+dbmd.getDatabaseProductVersion());
            System.out.println("数据库驱动:"+dbmd.getDriverName());
            System.out.println("数据库驱动版本:"+dbmd.getDriverVersion());
            System.out.println("用户名:"+dbmd.getUserName());
            System.out.println("URL:"+dbmd.getURL());

            String sql = new StringBuffer()
                    .append(" select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            // 获取结果集元数据
            // 通过结果集获取到对应的结果集元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            int count = rsmd.getColumnCount();
            System.out.println("结果集中共有:"+count+"列");
            for(int i = 1; i <= count; i++){
                System.out.println("第"+i+"列的类型:"+rsmd.getColumnTypeName(i));
                System.out.println("第"+i+"列所在的表:"+rsmd.getTableName(i));
                System.out.println("第"+i+"列的列名:"+rsmd.getColumnName(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,rs);
        }
        return null;
    }

    public static Integer insertReturnPrimaryKey(User user){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer id = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_user ")
                    .append("   (username,password,phone,address) ")
                    .append(" values ")
                    .append("   (?,?,?,?)")
                    .toString();
            ps = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getPhone());
            ps.setString(4,user.getAddress());
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            while (rs.next()){
                id = rs.getInt(1);
            }
            ResultSetMetaData rsmd = rs.getMetaData();
            for(int i = 1; i <= rsmd.getColumnCount(); i++){
                // 此时可以发现,MySQL为自动生成的列定义的字段名为:GENERATED_KEY
                System.out.println("第"+i+"列的列名为:"+rsmd.getColumnName(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,rs);
        }
        return id;
    }

}
